﻿using System.Linq;
using System.Web.Mvc;
using CID.Domain.Audit;
using CID.Domain.Users;
using CID.Web.Models.Audit;
using CID.Web.Support;
using NailsFramework.IoC;
using NailsFramework.Persistence;
using NailsFramework.UserInterface;


namespace CID.Web.Controllers
{
    [CIDAuthorize(UserRole.Architect, UserRole.Manager, UserRole.Operator, UserRole.ReleaseManager)]
    public class DeploymentLogController : NailsController
    {
        [Inject]
        public IBag<ReleaseDeploymentLog> DeploymentLogs { private get; set; }

        public ViewResult Index()
        {
            return Index(null, null, null, null);
        }

        [HttpPost]
        public ViewResult Index(int? releaseId = null, int? environmentId = null, int? responsibleId = null, DeploymentStatus? status = null)
        {
            IQueryable<ReleaseDeploymentLog> deploymentLogs = DeploymentLogs;
            if (releaseId.HasValue) deploymentLogs = deploymentLogs.Where(x => x.Release.Id == releaseId.Value);
            if (environmentId.HasValue) deploymentLogs = deploymentLogs.Where(x => x.Environment.Id == environmentId.Value);
            if (responsibleId.HasValue) deploymentLogs = deploymentLogs.Where(x => x.Responsible.Id == responsibleId.Value);
            if (status.HasValue) deploymentLogs = deploymentLogs.Where(x => x.Status == status.Value);
            return View(new ReleaseDeploymentLogListViewModel(deploymentLogs));
        }

        public ViewResult Details(int id)
        {
            return View(DeploymentLogs.GetById(id));
        }
    }
}